package com.sx.pipeline.ui.group

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.hjq.base.BaseAdapter.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.sx.pipeline.R
import com.sx.pipeline.adapter.GroupManagerAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.arouth.RoutePath.Chat.CHAT_GROUP_MANAGER
import com.sx.pipeline.databinding.ActGroupManagerBinding
import com.sx.pipeline.repos.http.res.GroupBean
import io.rong.imkit.userinfo.RongUserInfoManager
import io.rong.imlib.model.Conversation

@Route(path = CHAT_GROUP_MANAGER)
class GroupManagerAct : CYDataBindingBaseActivity<ActGroupManagerBinding,GroupManagerVm>(R.layout.act_group_manager) {
    @Autowired
    @JvmField
    var id: String =""
    var adapter= GroupManagerAdapter(15)
    override fun initViewModel() {
        super.initViewModel()
        mViewModel.groupLiveListData.observe(this){
            it?.map { GroupBean(it) }?.toMutableList()?.let {
                    //有管理人权限
                    if(it.size>13){
                        it.add(13,GroupBean(tempType = 1))
                        if(mViewModel.hasAdmin){
                            it.add(14,GroupBean(tempType = 2))
                        }
                    }else{
                        it.add(GroupBean(tempType = 1))
                        if(mViewModel.hasAdmin){
                            it.add(GroupBean(tempType = 2))
                        }
                    }
                adapter.setList(it)
            }
            if(mViewModel.hasAdmin){
                mBinding.tvOut.visibility= View.GONE
                mBinding.tvDelete.visibility= View.VISIBLE
            }
        }
        mBinding.tvReport.setOnClickListener {
            ARouter.getInstance().build(RoutePath.Chat.CHAT_REPORT)
                .withInt("type", 0)
                .withString("rId", id)
                .navigation()
        }
    }
    override fun initData() {
        super.initData()
        ImmersionBar.with(this)
            .fitsSystemWindows(true)
            .statusBarColor(R.color.color_0188ff)
            .statusBarDarkFont(false)
            .init()
        mBinding.list.layoutManager=GridLayoutManager(this,5)
        mBinding.list.adapter=adapter
//        mViewModel.groupInfoById(id)
        mBinding.titleBar.setOnTitleBarListener(object: OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                super.onLeftClick(titleBar)
                finish()
            }
        })
        setTitle()
        adapter.setOnItemClickListener{adapter, view, position ->
            var bean=adapter.data[position] as GroupBean
            if(bean.itemType==0){
                //详情
                bean.contact?.userUnitId?.let {
                    ARouter.getInstance().build(RoutePath.Chat.CHAT_PERSONAL)
                        .withInt(IntentKey.ID, it).navigation()
                }
            }else {
                ARouter.getInstance().build(RoutePath.Chat.CHAT_GROUP_OPTION)
                    .withInt("optionType", bean.itemType)
                    .withString("id", id)
                    .navigation()
            }
        }

        mBinding.tvMore.setOnClickListener {
            ARouter.getInstance().build(RoutePath.Chat.CHAT_GROUP_LIST)
                .withString("id", id)
                .navigation()
        }
        mBinding.tvOut.setOnClickListener {
            //退出群聊
            XPopup.Builder(this)
                .asConfirm("提示","确定要退出群聊吗？"){
                    mViewModel.exitGroup(id.toInt())
                }.show()
        }
        mBinding.tvDelete.setOnClickListener {
            //删除群聊
            XPopup.Builder(this)
                .asConfirm("提示","确定要解散该群聊吗？"){
                    mViewModel.cleanGroup(id)
                }.show()
        }
    }

    private fun setTitle() {
        val group = RongUserInfoManager.getInstance().getGroupInfo(id)
        if(group!=null){
            mBinding.titleBar.title=group.name
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.groupInfoById(id)
    }
}